package com.zkj.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zkj.dto.DishDTO;
import com.zkj.dto.DishPageQueryDTO;
import com.zkj.entity.Dish;
import com.zkj.result.Result;
import com.zkj.service.IDishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author zkj
 * @since 2024-08-22
 */
@RestController
@RequestMapping("/admin/dish")
@Component("adminDishController")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private IDishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dish) {
        dishService.addDish(dish);

        Long categoryId = dish.getCategoryId();
        String key = "dish_" + categoryId;
        redisTemplate.delete(key);

        return Result.success();
    }

    @ApiOperation("菜品分页")
    @GetMapping("/page")
    public Result pageListPaging(@RequestParam(required = false) String name, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) Integer status) {
        return dishService.pageListPaging(page, pageSize, name, categoryId, status);
    }

    @ApiOperation("通过分类取菜品")
    @GetMapping("/list")
    public Result getDishByCategoryId(@RequestParam(required = false) Long categoryId) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        if (categoryId != null) {
            queryWrapper.eq(Dish::getCategoryId, categoryId);
        }
        List<Dish> list = dishService.list(queryWrapper);
        return Result.success(list);
    }

    @GetMapping("/test")
    public String test() {
        redisTemplate.opsForValue().set("test", "test");

        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("test11", "test", "test");

        return (String) redisTemplate.opsForValue().get("test");
    }


    @ApiOperation("修改菜品")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dish) {
        dishService.updateDish(dish);
        deleteRedis("dish_*");
        return Result.success();
    }

    @ApiOperation("修改菜品状态")
    @PostMapping("/status/{status}")
    public Result updateDishStatus(@PathVariable Integer status, Long id) {
        deleteRedis("dish_*");

        return dishService.updateDishStatus(status, id);
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result getDishById(@PathVariable Long id) {
        return dishService.getDishById(id);
    }

    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result deleteDish(@RequestParam Long ids) {
        deleteRedis("dish_*");
        return dishService.deleteDish(ids);
    }


    /**
     * 删除redis缓存
     * @param pattern 删除缓存传入缓存key的匹配规则
     */
    private void deleteRedis(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
